package jsp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/count")
public class CountServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 从session中获取当前访问次数
        Integer count = (Integer) req.getSession().getAttribute("count");
        // 判断是否是第一次访问,如果count为null,说明尚未访问过
        if (count == null) {
            // 此时设置访问次数为1
            count = 1;
        } else {
            // 否则在原次数上+1
            count++;
        }
        // 将新的访问次数存储到session中
        req.getSession().setAttribute("count", count);
        req.getRequestDispatcher("/jsp/count.jsp").forward(req, resp);
    }
}
